package lib;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class TwoMenuFilter extends WebFilter  {

	@Override
	public void lay_noi_dung_web(String url, String str1, String str2) {
		// TODO Auto-generated method stub
		String ch1="";
		String ch2="";
		String link="";
		ConnectionWeb con = new ConnectionWeb();
		// do parsing
		Document root;
		root =con.getInfo(url);
		List<Element> nodes=	con.executeXpath(root, str1,20);
		
		for(int chiso=0;chiso<nodes.size();chiso++)	
		{		
				Element tr = nodes.get(chiso);
				System.out.println(tr.toString());
				
				List<Element> lNode= tr.getElementsByTag("a");
				for(int i= 0; i< lNode.size();i++)
				{
					Element t1=lNode.get(i);
					ch1=t1.text();
					String tlink=StringEdit.EditLink(url,t1.attr("href"));
					tlink= StringEdit.EditLink(url, tlink);
					Document root2 = con.getInfo(tlink);
					
					System.out.println(i);
					List<Element> ln2 =	con.executeXpath(root2, str2,1);
					boolean flag2=false;
					if(ln2.size()>0)
					{
						for(int chiso2=0;chiso2<ln2.size();chiso2++)
						{
							Element t6= ln2.get(chiso2);
				
							List<Element> l_menu2 = t6.children();
							List<Element> l_menu3 = t6.getElementsByTag("a");
							if(l_menu3.size()>l_menu2.size())
							{
								for(int j=0;j<l_menu3.size();j++)
								{	
									Element t7=  l_menu3.get(j);
									
									if(t7.tagName().equals("a")==true)
									{
										link = t7.attr("href");
										link = StringEdit.EditLink(url, link);
										ch2= ch1+"/"+t7.text();						
										ch2= StringEdit.EditDir(ch2);
										this.setLink(ch2, link);
										flag2=true;
									}
									else
									{
										if(t7.getElementsByTag("a").size()>0)
										{
											Element t8= t7.getElementsByTag("a").get(0);
											link = t8.attr("href");
											link = StringEdit.EditLink(url, link);
											ch2= ch1+"/"+t8.text();						
											ch2= StringEdit.EditDir(ch2);
											this.setLink(ch2, link);
											flag2=true;
										}
									}	
								}
							}
							else
							{
								for(int j=0;j<l_menu2.size();j++)
								{	
									Element t7=  l_menu2.get(j);
									
									if(t7.tagName().equals("a")==true)
									{
										link = t7.attr("href");
										link = StringEdit.EditLink(url, link);
										ch2= ch1+"/"+t7.text();						
										ch2= StringEdit.EditDir(ch2);
										this.setLink(ch2, link);
										flag2=true;
									}
									else
									{
										if(t7.getElementsByTag("a").size()>0)
										{
											Element t8= t7.getElementsByTag("a").get(0);
											link = t8.attr("href");
											link = StringEdit.EditLink(url, link);
											ch2= ch1+"/"+t8.text();						
											ch2= StringEdit.EditDir(ch2);
											this.setLink(ch2, link);
											flag2=true;
										}
									}	
								}
							}
						}
					}
					else
					{
						link=tlink;
						ch2=ch1;
						ch2= StringEdit.EditDir(ch2);
						this.setLink(ch2, link);
						flag2=true;
					}
					if(flag2==false)
					{
						link=tlink;
						ch2=ch1;
						ch2= StringEdit.EditDir(ch2);
						this.setLink(ch2, link);
					}
				}	
		
		}
			
	}
	
}
